// basic playground for testing text editor while developing it

const {
  ui: {
    Pane,
    form: {
      FocusElement
    }
  },
  util: {
    tuiApp
  }
} = require('tui-lib')

const TuiTextEditor = require('./index')

const defaultText = `Hello, world!
This is a test value.
I think it is good fun.

AWOO
GAAA  1
AAAA  2
AAAA  3
AAAA  4
AAAA  5
AAAA  6
AAAA  7
AAAA  8
AAAA  9
AAAA 10
AAAA 11
AAAA 12
AAAA 13
AAAA 14
AAAA 15
AAAA 16
AAAA 17
AAAA 18
AAAA 19
AAAA 20`

class AppElement extends FocusElement {
  constructor() {
    super()

    this.pane = new Pane()
    this.addChild(this.pane)

    this.editor = new TuiTextEditor()
    this.pane.addChild(this.editor)

    this.editor.clearSourceAndLoadText(defaultText)
  }

  selected() {
    this.root.select(this.editor)
  }

  fixLayout() {
    this.fillParent()
    this.pane.fillParent()
    this.editor.fillParent()
    this.editor.fixLayout()
  }

  keyPressed(keyBuf) {
    if (keyBuf[0] === 0x03) {
      this.emit('quitRequested')
    } else if (keyBuf[0] === 0x07) {
      this.editor.showStatusMessage('You pressed ^G! Nice job! Random number: ' + Math.random())
    }
  }
}

tuiApp(({root, quitProgram}) => {
  const app = new AppElement()

  app.on('quitRequested', quitProgram)

  root.addChild(app)
  root.select(app)

  setTimeout(() => app.scheduleDrawWithoutPropertyChange())
})
